package cn.edu.nju.software.action.book;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import cn.edu.nju.software.bean.show.BookShow;
import cn.edu.nju.software.bean.show.CategoryShow;
import cn.edu.nju.software.bean.storage.Book;
import cn.edu.nju.software.service.intf.BookService;
import cn.edu.nju.software.util.Constants;
import cn.edu.nju.software.util.SessionOperator;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ViewBookAction extends ActionSupport implements SessionAware {

	private int id;
	private BookShow bookShow;
	private BookService bookService;
	private Map<String, Object> session;

	public void setId(int id) {
		this.id = id;
	}
	
	public BookShow getBookShow() {
		return bookShow;
	}

	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public String execute() throws Exception {
		if ((bookShow = SessionOperator.searchBookInSession(session, id)) == null) {
			@SuppressWarnings("unchecked")
			Map<Integer, CategoryShow> categoriesMap = (Map<Integer, CategoryShow>) session.get(Constants.SESSION_ALL_CATEGORIES);
			Book book = bookService.getBookById(id);
			bookShow = new BookShow(book, categoriesMap.get(book.getCategoryID()));
		}
		return SUCCESS;
	}
}
